


# pages/profile.py - 个人资料页面
import streamlit as st
from st_pages import add_page_title
import streamlit_authenticator as stauth

# 检查认证状态
if 'authentication_status' not in st.session_state or not st.session_state.authentication_status:
    st.error("请先登录!")
    st.stop()

add_page_title()

st.markdown("### 👤 个人资料管理")

# 获取当前用户信息
current_user = st.session_state.get('username', 'unknown')
current_name = st.session_state.get('name', 'Unknown User')

# 用户基本信息
st.subheader("ℹ️ 基本信息")

col1, col2 = st.columns([1, 2])

with col1:
    # 头像区域
    st.image("https://via.placeholder.com/150x150.png?text=Avatar", width=150)
    if st.button("更换头像"):
        st.info("头像上传功能待开发")

with col2:
    # 基本信息表单
    with st.form("profile_form"):
        display_name = st.text_input("显示名称", value=current_name)
        email = st.text_input("邮箱地址", value=f"{current_user}@example.com")
        phone = st.text_input("电话号码", value="")
        department = st.selectbox(
            "部门",
            ["技术部", "市场部", "销售部", "人事部", "财务部"],
            index=0
        )
        bio = st.text_area("个人简介", placeholder="介绍一下您自己...")
        
        submitted = st.form_submit_button("更新资料")
        
        if submitted:
            st.success("个人资料已更新!")

# 账户安全
st.divider()
st.subheader("🔒 账户安全")

tab1, tab2, tab3 = st.tabs(["密码设置", "安全选项", "登录历史"])

with tab1:
    st.markdown("#### 修改密码")
    
    with st.form("password_form"):
        current_password = st.text_input("当前密码", type="password")
        new_password = st.text_input("新密码", type="password")
        confirm_password = st.text_input("确认新密码", type="password")
        
        password_submit = st.form_submit_button("修改密码")
        
        if password_submit:
            if new_password != confirm_password:
                st.error("新密码与确认密码不匹配!")
            elif len(new_password) < 6:
                st.error("密码长度至少为6位!")
            else:
                st.success("密码修改成功!")

with tab2:
    st.markdown("#### 双重认证")
    
    two_factor_auth = st.checkbox("启用双重认证 (2FA)", value=False)
    
    if two_factor_auth:
        st.info("🔐 双重认证已启用，您的账户更加安全!")
        auth_method = st.radio(
            "认证方式",
            ["短信验证码", "邮箱验证码", "认证器应用"],
            index=0
        )
    
    st.markdown("#### 安全提醒")
    
    login_alerts = st.checkbox("新设备登录提醒", value=True)
    suspicious_activity = st.checkbox("可疑活动提醒", value=True)
    password_expiry = st.checkbox("密码到期提醒", value=False)

with tab3:
    st.markdown("#### 最近登录记录")
    
    # 模拟登录历史数据
    login_history = [
        {"时间": "2024-01-15 09:30:22", "设备": "Chrome/Windows", "IP地址": "192.168.1.100", "状态": "✅ 成功"},
        {"时间": "2024-01-14 14:15:33", "设备": "Safari/macOS", "IP地址": "192.168.1.101", "状态": "✅ 成功"},
        {"时间": "2024-01-13 11:45:12", "设备": "Chrome/Windows", "IP地址": "192.168.1.100", "状态": "❌ 失败"},
        {"时间": "2024-01-12 16:20:45", "设备": "Firefox/Linux", "IP地址": "192.168.1.102", "状态": "✅ 成功"},
    ]
    
    for record in login_history:
        col1, col2, col3, col4 = st.columns([2, 2, 2, 1])
        with col1:
            st.text(record["时间"])
        with col2:
            st.text(record["设备"])
        with col3:
            st.text(record["IP地址"])
        with col4:
            st.text(record["状态"])

# 偏好设置
st.divider()
st.subheader("🎛️ 偏好设置")

col1, col2 = st.columns(2)

with col1:
    st.markdown("#### 界面偏好")
    default_page = st.selectbox(
        "登录后默认页面",
        ["首页", "仪表板", "数据分析"],
        index=0
    )
    
    items_per_page = st.slider(
        "每页显示条数",
        min_value=10,
        max_value=100,
        value=25,
        step=5
    )

with col2:
    st.markdown("#### 通知偏好")
    email_frequency = st.selectbox(
        "邮件通知频率",
        ["实时", "每小时", "每天", "每周", "关闭"],
        index=2
    )
    
    timezone = st.selectbox(
        "时区设置",
        ["Asia/Shanghai", "Asia/Tokyo", "UTC", "America/New_York"],
        index=0
    )

# 数据导出
st.divider()
st.subheader("📤 数据导出")

st.info("您可以导出个人数据用于备份或迁移")

col1, col2, col3 = st.columns(3)

with col1:
    if st.button("导出个人资料", use_container_width=True):
        st.success("个人资料导出请求已提交!")

with col2:
    if st.button("导出活动日志", use_container_width=True):
        st.success("活动日志导出请求已提交!")

with col3:
    if st.button("导出全部数据", use_container_width=True):
        st.success("全部数据导出请求已提交!")

# 账户管理
st.divider()
st.subheader("⚠️ 账户管理")

st.warning("危险操作区域 - 请谨慎操作")

col1, col2 = st.columns(2)

with col1:
    if st.button("注销所有设备", type="secondary"):
        st.info("已从所有设备注销登录状态")

with col2:
    if st.button("删除账户", type="secondary"):
        st.error("账户删除功能需要管理员确认")